const mongoose = require('mongoose')
const validator = require('validator')

const articleSchema = new mongoose.Schema(
  {
    title: {
      type: String,
      required: [true, '文章分类不能为空'],
      unique: true,
      max: 200,
    },
    summary: String,
    content: String,
    coverImage: {
      type: String,
      validate: [validator.isURL, '封面url格式不正确'],
    },
    category: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Category',
    },
    tags: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Tag',
      },
    ],
  },
  { timestamps: true },
)

const Article = mongoose.model('Article', articleSchema)

module.exports = Article
